<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <!-- Logo图标文字区域 -->
      <div class="logo">
        <img src="../assets/image/logo_01.jpg" alt />
        <span class="logo_title">通用后台管理系统</span>
      </div>
      <!-- 折叠按钮 -->
      <div class="collapse-btn" @click="collapseChage">
        <i v-if="!collapse" class="el-icon-s-unfold"></i>
        <i v-else class="el-icon-s-fold"></i>
      </div>
      <!-- 右边区域 -->
      <div class="header-right">
        <div class="header-user-con">
          <!-- 全屏显示 -->
          <div class="btn-fullscreen" @click="handleFullScreen">
            <el-tooltip effect="dark" :content="fullscreen?`取消全屏`:`全屏`" placement="bottom">
              <i class="el-icon-rank"></i>
            </el-tooltip>
          </div>
          <!-- 消息中心 -->
          <div class="btn-bell">
            <el-tooltip
              effect="dark"
              :content="message?`有${message}条未读消息`:`消息中心`"
              placement="bottom"
            >
              <router-link to="/tabs">
                <i class="el-icon-bell"></i>
              </router-link>
            </el-tooltip>
            <span class="btn-bell-badge" v-if="message"></span>
          </div>
          <!-- 用户头像 -->
          <div class="user-avator">
            <img src="../assets/image/avator.jpg" />
          </div>
          <!-- 用户名下拉菜单 -->
          <el-dropdown class="user-name" trigger="click" @command="handleCommand">
            <span class="el-dropdown-link">
              {{username}}
              <i class="el-icon-caret-bottom"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <a href="https://www.baidu.com" target="_blank">
                <el-dropdown-item>跳转前台</el-dropdown-item>
              </a>
              <el-dropdown-item divided command="loginout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
      <!-- <el-button type="info" @click="logout">退出</el-button> -->
    </el-header>
    <el-container>
      <!-- 菜单区域 -->
      <el-aside :width="collapse ? '65px' : '250px'">
        <el-menu background-color="#324157" text-color="#fff" active-text-color="#2d8cf0" unique-opened :collapse="collapse" :collapse-transition="false" router :default-active="$route.path">
          <!-- 一级菜单 -->
          <el-menu-item index="/index">
            <i class="el-icon-s-home"></i>
            <span slot="title">系统首页</span>
          </el-menu-item>
          <!-- 循环菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id">
              <template slot="title">
                <i class="el-icon-setting"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
            
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体区域 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      collapse: false,
      fullscreen: false,
      message: 2,
      name: "admin1",
      menuList:[],
      iconObj:{
        '125':'el-icon-user-solid',
        '101':'el-icon-present',
        '103':'el-icon-key',
        '102':'el-icon-edit-outline',
        '145':'el-icon-data-line'
      }
    };
  },
  created(){
    this.getMenuList();
  },
  computed: {
    username() {
      let username = window.sessionStorage.getItem("username");
      return username ? username : this.name;
    },
  },
  methods: {
    // 用户名下拉菜单选择事件
    handleCommand(command) {
      if (command == "loginout") {
        this.logout();
      }
    },
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    collapseChage() {
      this.collapse = !this.collapse;
    },
    async getMenuList(){
      const {data: res} = await this.$http.get('menus');
      if(res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menuList = res.data;
    },
    handleFullScreen() {
      let element = document.documentElement;
      if (this.fullscreen) {
        if (document.exitFullscreen) {
          document.exitFullscreen();
        } else if (document.webkitCancelFullScreen) {
          document.webkitCancelFullScreen();
        } else if (document.mozCancelFullScreen) {
          document.mozCancelFullScreen();
        } else if (document.msExitFullscreen) {
          document.msExitFullscreen();
        }
      } else {
        if (element.requestFullscreen) {
          element.requestFullscreen();
        } else if (element.webkitRequestFullScreen) {
          element.webkitRequestFullScreen();
        } else if (element.mozRequestFullScreen) {
          element.mozRequestFullScreen();
        } else if (element.msRequestFullscreen) {
          // IE11
          element.msRequestFullscreen();
        }
      }
      this.fullscreen = !this.fullscreen;
    
    
    },
  },
  mounted() {
    if (document.body.clientWidth < 1500) {
      this.collapseChage();
    }
  },
};
</script>

<style lang="less" scoped>
.el-header {
  background-color: #242f42;
  height: 70px !important;
  color: #fff;
  font-size: 22px;
  padding: 0px;
}
.el-aside {
  background-color: rgb(50, 65, 87);
  .el-menu{
    border-right: none;
  }
}
.el-main {
  background-color: #f0f0f0;
}
.home-container {
  height: 100%;
}
.el-header .logo {
  float: left;
  width: 250px;
  line-height: 70px;
  display: flex;
  align-items: center;
}
.logo img {
  display: block;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  float: left;
  margin-left: 5px;
}
.logo_title {
  margin-left: 10px;
}
.collapse-btn {
  float: left;
  padding: 0 21px;
  cursor: pointer;
  line-height: 70px;
}
.collapse-btn:hover {
  background: rgb(40, 52, 70);
}
.header-right {
  float: right;
  padding-right: 50px;
}
.header-user-con {
  display: flex;
  height: 70px;
  align-items: center;
}
.btn-fullscreen {
  transform: rotate(45deg);
  margin-right: 5px;
  font-size: 24px;
}
.btn-bell,
.btn-fullscreen {
  position: relative;
  width: 30px;
  height: 30px;
  text-align: center;
  border-radius: 15px;
  cursor: pointer;
}
.btn-bell-badge {
  position: absolute;
  right: 0;
  top: -2px;
  width: 8px;
  height: 8px;
  border-radius: 4px;
  background: #f56c6c;
  color: #fff;
}
.btn-bell .el-icon-bell {
  color: #fff;
}
.user-name {
  margin-left: 10px;
}
.user-avator {
  margin-left: 20px;
}
.user-avator img {
  display: block;
  width: 40px;
  height: 40px;
  border-radius: 50%;
}
.el-dropdown-link {
  color: #fff;
  cursor: pointer;
}
.el-dropdown-menu {
  padding: 5px 0;
}
.el-dropdown-menu--small {
  padding: 6px 0;
}
.el-dropdown-menu__item {
  text-align: center;
  line-height: 27px;
}
.el-dropdown-menu--small
  .el-dropdown-menu__item.el-dropdown-menu__item--divided {
  margin-top: 4px;
}
</style>